package pikasial.images.traitements;

import java.awt.image.BufferedImage;

public class GrayScaleTraitement implements Traitement {

	protected BufferedImage source;

	public GrayScaleTraitement(BufferedImage bi) {
		source = bi;
	}


	public BufferedImage executeTraitement() {
		int w = source.getWidth();
	    int h = source.getHeight();
	    BufferedImage bImage2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
	    for (int i = 0; i < w; i++) {
	      for (int j = 0; j < h; j++) {
	        int p = source.getRGB(i, j);
	        int a = (((p >> 16) & 0xff) + ((p >> 8) & 0xff) + (p & 0xff)) / 3;
	        bImage2.setRGB(i, j, (0xff << 24) | (a << 16) | (a << 8) | a);
	      }
	    }
		return bImage2;
	}


	public BufferedImage undoChanges() {
		return source;
	}
}